import {auth, authInfo} from '../../apis/user'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    show: false,
    region: ['', '', ''],
    customItem: '全部',
    state: -1,
    check: '0',
    detailAddr: '',
    info: {},
    tag: false,
    width: app.globalData.windowWidth,
    fileList: [],
  },

  // 文件上传
  afterRead(event) {
    const fileList = this.data.fileList;
    if (fileList != null && fileList.length > 0) {
      wx.showToast({
        title: '只能上传一张图片',
      })
    }
    const { file } = event.detail;
    const that = this
    wx.showLoading({
      title: '上传中...',
    })
    const url = app.globalData.baseUrl + '/file/upload';
    wx.uploadFile({
      url: url,
      filePath: file.url,
      name: 'file',
      header: {
        token: app.globalData.token
      },
      success(res) {
        // 上传完成需要更新 fileList
        let obj = JSON.parse(res.data)
        fileList.push({
          url: obj.data,
          type:"image"
        })
        that.setData({ fileList });
        wx.hideLoading()
      },fail(res) {
        wx.hideLoading();
        wx.showToast({
          title: '文件上传失败',
        })
      }
    });
  },

  delete(event) {
    this.setData({
      fileList: []
    })
  },

  goInfo() {
    wx.redirectTo({
      url: '/pages/myPage/myPage',
    })
  },

 //日期
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
  },

  phoneOnChange(event) {
    this.setData({
      phone: event.detail,
    });
  },

  nameOnChange(event) {
    this.setData({
      name: event.detail,
    });
  },

  detailAddrOnChange(event) {
    this.setData({
      detailAddr: event.detail,
    });
  },

  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },

  async formSubmit(e) {
    let cUrl = '';
    if (this.data.fileList.length !=0) {
      cUrl = this.data.fileList[0].url
    }
    
    let data = {
      name: this.data.name,
      detailAddr: this.data.detailAddr,
      phone: this.data.phone,
      province: this.data.region[0],
      city: this.data.region[1],
      town: this.data.region[2],
      certificationUrl: cUrl
    }
    if (data.name == '') {
      wx.showToast({
        title: '请填写姓名',
      })
    } else if (data.phone == '') {
      wx.showToast({
        title: '请填写手机',
      })
    } else if (data.province == '' || data.city == '' || data.town == '') {
      wx.showToast({
        title: '请选择省区市',
      })
    } else if (data.detailAddr == '') {
      wx.showToast({
        title: '请填写详细地址',
      })
    } else if (data.certificationUrl == '') {
      wx.showToast({
        title: '请上传身份证',
      })
    } else {
      const res = await auth(data);
      if (res.code == '200') {
        wx.showToast({
          title: '提交成功'
        })
        wx.redirectTo({
          url: '/pages/myPage/myPage',
        })
      } else {
        wx.showToast({
          title: res.message,
        })
      }
    }
    
  },

  async initData() {
    const res = await authInfo();
    if (res.code == 200) {
      app.globalData.authInfo = res.data.info;
    } else {
      console.log(res.message);
      return ;
    }
    const info = app.globalData.authInfo;
    if (info != null) {
      this.setData({
        name: info.name,
        phone: info.phone,
        state: info.state,
        region: [info.province, info.city, info.town],
        detailAddr: info.detailAddr,
        fileList: [{
          url: info.certificationUrl,
          type: "image"
        }]
      })
    }
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取地理位置，上报信息时需要进行位置检验
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})